from typing import List


class Solution:
    def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
        n = len(bloomDay)

        def f(days: int) -> bool:
            bouquets = flowers = 0
            for bloom in bloomDay:
                if bloom <= days:
                    flowers += 1
                    if flowers == k:
                        bouquets += 1
                        if bouquets == m:
                            break
                        flowers = 0
                else:
                    flowers = 0
            return bouquets == m

        l = 1
        r = max(bloomDay)
        flag = False
        while l <= r:
            mid = (l + r) >> 1
            if f(mid):
                flag = True
                r = mid - 1
            else:
                l = mid + 1
        return r + 1 if flag else -1

if __name__ == '__main__':
    s = Solution()
    r = s.minDays(bloomDay = [1,10,3,10,2], m = 3, k = 2)
    print(r)